import { defineStore } from 'pinia'
import { ALL_CATEGORY_ITEM } from '@/constants'
import { useCategoryStore } from './category'
import { isMobileTerminal } from '@/utils/flexible'

export const useAppStore = defineStore('app', {
  state: () => ({
    currentCategory: ALL_CATEGORY_ITEM,
    searchText: '',
    routerType: 'none'
  }),
  getters: {
    /**
     * @returns {number} 当前分类的索引
     */
    currentCategoryIndex() {
      const categoryStore = useCategoryStore()
      return categoryStore.categorys.findIndex(
        (item) => item.id === this.currentCategory.id
      )
    },
    /**
     * 当前routerType
     */
    currentRouterType() {
      return !isMobileTerminal.value ? 'none' : this.routerType
    }
  },
  actions: {
    /**
     * 切换分类
     */
    changeCategory(category) {
      this.currentCategory = category
    },
    /**
     * 修改搜索文本
     */
    changeSearchText(searchText) {
      this.searchText = searchText
    },
    /**
     * 修改路由类型
     */
    changeRouterType(routerType) {
      this.routerType = routerType
    }
  }
})
